package be.teletask.onvif.parsers;

import be.teletask.onvif.models.GetAudioOutputs;
import be.teletask.onvif.models.OnvifMediaProfile;
import be.teletask.onvif.responses.OnvifResponse;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tomas Verhelst on 04/09/2018.
 * Copyright (c) 2018 TELETASK BVBA. All rights reserved.
 */
public class GetAudioOutputsParser extends OnvifParser<List<GetAudioOutputs>> {

    //Constants
    public static final String TAG = GetAudioOutputsParser.class.getSimpleName();
    private static final String KEY_AUDIO_OUT_PUTS = "AudioOutputs";
    private static final String ATTR_TOKEN = "token";

    @Override
    public List<GetAudioOutputs> parse(OnvifResponse response) {
        List<GetAudioOutputs> audioOutputs = new ArrayList<>();

        try {
            getXpp().setInput(new StringReader(response.getXml()));
            eventType = getXpp().getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    String tagName = getXpp().getName();
                    if (KEY_AUDIO_OUT_PUTS.equals(tagName)) {
                        // 获取trt:Profiles的token属性
                        String profilesToken = getXpp().getAttributeValue(null, ATTR_TOKEN);

                        audioOutputs.add(new GetAudioOutputs(profilesToken));
                    }
                }

                eventType = getXpp().next();
            }
        } catch (XmlPullParserException | IOException e) {
            e.printStackTrace();
        }

        return audioOutputs;
    }

}
